/**
 * Created by zhijie on 2016/12/15.
 */
/**
 * Created by hezhijie on 2016/11/22.
 */
var agentListModule = angular.module("agentList",[]);
agentListModule.controller("agentListCtrl", ['$scope',
    function ($scope) {

        var setting={
            view: {
                dblClickExpand: false,
                showLine: true,
                selectedMulti: false
            },
            data: {
                key:{
                    name:"nickName"
                },
                simpleData: {
                    enable:true,
                    idKey: "id",
                    pIdKey: "superAgentId",
                    rootPId: "0"
                }
            },
            callback: {
                beforeClick: function(treeId, treeNode) {
                    $scope.agent=treeNode;
                    $scope.showSupperAgentNickName=false;
                    $scope.$apply();
                    return true;
                }
            }
        };
        var zTreeObj;
        $scope.init=function(){
            $scope.showSupperAgentNickName=false;
            $scope.queryAgentList()
        }

        $scope.queryAgentList=function(){
            $post(basePath+"/rest/agent/_query",{},function(result){
                $scope.resultValue=result.resultValue;
                $scope.$apply();
                $.fn.zTree.init($("#tree"), setting, $scope.resultValue.list);
                zTreeObj=$.fn.zTree.getZTreeObj("tree");
            })
        }

        $scope.saveAgentDetail = function () {
            if ($scope.agent.id) {
                $post(basePath + "/rest/agent/_update", $scope.agent, function (result) {
                    showMessage("保存成功");
                    zTreeObj.updateNode($scope.agent,false);
                })
            } else {
                $post(basePath + "/rest/agent/_add", $scope.agent, function (result) {
                    zTreeObj.addNodes(zTreeObj.getSelectedNodes()[0],$scope.agent)
                })
            }
        }

        $scope.showAddView=function(){
            $scope.agent={};
            if(zTreeObj.getSelectedNodes().length==1){
                $scope.agent.superAgentId=zTreeObj.getSelectedNodes()[0].id;
                $scope.agent.superAgentNickName=zTreeObj.getSelectedNodes()[0].nickName;
                $scope.showSupperAgentNickName=true;
            }else {
                showMessage("请选择一个代理商")
            }
        }
        
        $scope.showEditView=function () {
            $scope.agent={};
            if(zTreeObj.getSelectedNodes().length==1){
                $scope.agent=zTreeObj.getSelectedNodes()[0];
                $scope.showSupperAgentNickName=false;
            }else {
                showMessage("请选择一个代理商")
            }
        }
        $scope.init();
    }
]);
